import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/product_plateconsumption_odoo_api.dart';
import 'package:flutter_admin/api/odoo/product_template_odoo_api.dart';
import 'package:flutter_admin/models/odoo/product_plateconsumption.dart';
import 'package:flutter_admin/models/odoo/product_template.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select_date.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:intl/intl.dart';
import '../../../generated/l10n.dart';

class ProductTemplateEdit extends StatefulWidget {
  ProductTemplateEdit({this.productTemplate});

  final ProductTemplate? productTemplate;

  @override
  _ProductTemplateEditState createState() => _ProductTemplateEditState();
}

class _ProductTemplateEditState extends State<ProductTemplateEdit> {
  late ProductTemplate productTemplate;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  //late List<SelectOptionVO> activeList;

  List<SelectOptionVO> productList = [];

  List<SelectOptionVO> thicknessList = [];

  List<SelectOptionVO> stationList = [];

  List<SelectOptionVO> filledList = [];

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.productTemplate == null;
    this.productTemplate = widget.productTemplate ?? ProductTemplate();
    _loadSelectVO();
  }

  _loadSelectVO() async {
    if (isAdd!) {
      ResponseBodyApi res = await ProductTemplateOdooApi.getCode();
      if (res.success!) {
        productTemplate.defaultCode = res.data;
        setState(() {});
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            label: '板材编码',
            enable: false,
            value: productTemplate.defaultCode,
            onSaved: (v) {
              productTemplate.defaultCode = v;
            },
          ),
          CryInput(
            label: '板材名称',
            value: productTemplate.name,
            onSaved: (v) {
              productTemplate.name = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInputNum(
            label: '板材密度',
            value: productTemplate.density,
            onSaved: (v) {
              productTemplate.density = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInputNum(
            label: '顺序号',
            value: productTemplate.sequence,
            onSaved: (v) {
              productTemplate.sequence = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '备注',
            value: productTemplate.remark,
            onSaved: (v) {
              productTemplate.remark = v;
            },
          ),
          //
          // CrySelect(
          //   label: '填报人',
          //   value: productPlateconsumption.filledUid,
          //   dataList: filledList,
          //   onSaved: (v) {
          //     productPlateconsumption.filledUid = v;
          //   },
          // ),
          //
          // CryInput(
          //   label: '备注',
          //   value: productPlateconsumption.remark,
          //   onSaved: (v) {
          //     productPlateconsumption.remark = v;
          //   },
          // ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            // productPlateconsumption.filledDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now());
            ProductTemplateOdooApi.saveOrUpdate(productTemplate.toBCOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //package:flutter_admin/pages/common/cry(re s.message!);
                return;
              }
              if (res.data["code"] != 200) {
                CryUtils.message(res.data["message"]);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
